package com.tjhk.java.tech.thread.completable;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class FutureTest {
    public static void main(String[] args) {
        ExecutorService exc = Executors.newFixedThreadPool(2);
        Future<Integer> f1= exc.submit(()->getPrice1());
        Future<Integer> f2 = exc.submit(() -> getPrice2());
        exc.execute(()-> {
            try {
                save(f1.get());
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            } catch (ExecutionException e) {
                throw new RuntimeException(e);
            }
        });
        exc.execute(()-> {
            try {
                save(f2.get());
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            } catch (ExecutionException e) {
                throw new RuntimeException(e);
            }
        });
        System.out.println("done");
    }
    public static Integer getPrice1(){
        try {
            System.out.println("正在获取价格-1");
            Thread.sleep(3000);
            System.out.println("!!!!!!!!!!!");
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        return 10;
    }
    public static Integer getPrice2(){
        try {
            System.out.println("正在获取价格-2");
            Thread.sleep(30000);
            System.out.println("##########");
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        return 7878;
    }

    public static void save(Integer a){
        System.out.println("save: "+a);
    }
}
